/**
 * CRM项目模块统一导入配置文件
 * 使用静态路径进行导入，避免动态require
 */

const pathResolver = require('./path_resolver.js');

// 预先定义备用模块
const backupHelpers = {
  pageHelper: {
    url: () => {},
    showModal: (msg) => { console.error(msg); }
  },
  cloudHelper: {
    callCloud: async () => ({})
  },
  timeHelper: {
    time: () => new Date().toISOString()
  },
  validate: {
    checkRequired: (val, desc = '') => val ? '' : `${desc}不能为空`
  },
  fileHelper: {
    getExtByMime: () => 'unknown'
  },
  routerHelper: {
    navigateTo: (url) => wx.navigateTo({ url })
  }
};

const backupNpmPackages = {
  mobx: { observable: () => {}, action: () => {} },
  createStoreBindings: () => ({ destroyStoreBindings: () => {} }),
  storeBindingsBehavior: {},
  dayjs: () => ({ format: () => new Date().toISOString() })
};

// 直接加载模块配置
let Helpers = {};
let NpmPackages = {};

// 尝试从各种可能的路径加载module_config.js
try {
  // 1. 尝试最可能的路径
  const moduleConfig = require('../../../config/module_config.js');
  console.log('[path_import.js] 成功从 ../../../config/module_config.js 加载模块配置');
  
  if (moduleConfig.Helpers) Helpers = moduleConfig.Helpers;
  if (moduleConfig.NpmPackages) NpmPackages = moduleConfig.NpmPackages;
} catch (err1) {
  console.warn('[path_import.js] 从 ../../../config/module_config.js 加载失败');
  
  try {
    // 2. 尝试第二可能的路径
    const moduleConfig = require('../../config/module_config.js');
    console.log('[path_import.js] 成功从 ../../config/module_config.js 加载模块配置');
    
    if (moduleConfig.Helpers) Helpers = moduleConfig.Helpers;
    if (moduleConfig.NpmPackages) NpmPackages = moduleConfig.NpmPackages;
  } catch (err2) {
    console.warn('[path_import.js] 从 ../../config/module_config.js 加载失败');
    
    try {
      // 3. 尝试第三可能的路径
      const moduleConfig = require('./module_config.js');
      console.log('[path_import.js] 成功从 ./module_config.js 加载模块配置');
      
      if (moduleConfig.Helpers) Helpers = moduleConfig.Helpers;
      if (moduleConfig.NpmPackages) NpmPackages = moduleConfig.NpmPackages;
    } catch (err3) {
      console.error('[path_import.js] 所有路径都加载失败，使用备用模块');
    }
  }
}

// 加载基础辅助模块
let pageHelper, cloudHelper, timeHelper;

try {
  // 尝试加载pageHelper
  pageHelper = require('../../../helpers/page_helper.js');
} catch (err) {
  console.warn('[path_import.js] 加载 page_helper.js 失败，使用备用实现');
  pageHelper = backupHelpers.pageHelper;
}

try {
  // 尝试加载cloudHelper
  cloudHelper = require('../../../helpers/cloud_helper.js');
} catch (err) {
  console.warn('[path_import.js] 加载 cloud_helper.js 失败，使用备用实现');
  cloudHelper = backupHelpers.cloudHelper;
}

try {
  // 尝试加载timeHelper
  timeHelper = require('../../../helpers/time_helper.js');
} catch (err) {
  console.warn('[path_import.js] 加载 time_helper.js 失败，使用备用实现');
  timeHelper = backupHelpers.timeHelper;
}

/**
 * 获取全局辅助模块
 * @param {string} context - 调用上下文，可选值：'service', 'page'
 * @returns {object} 全局辅助模块配置
 */
function getGlobalHelpers(context = 'page') {
  // 返回结果对象
  return {
    pageHelper: Helpers.pageHelper || pageHelper || backupHelpers.pageHelper,
    cloudHelper: Helpers.cloudHelper || cloudHelper || backupHelpers.cloudHelper,
    timeHelper: Helpers.timeHelper || timeHelper || backupHelpers.timeHelper,
    validate: Helpers.validate || backupHelpers.validate,
    fileHelper: Helpers.fileHelper || backupHelpers.fileHelper,
    routerHelper: Helpers.routerHelper || backupHelpers.routerHelper,
    
    // NPM包
    mobx: NpmPackages.mobx || backupNpmPackages.mobx,
    createStoreBindings: NpmPackages.createStoreBindings || backupNpmPackages.createStoreBindings,
    storeBindingsBehavior: NpmPackages.storeBindingsBehavior || backupNpmPackages.storeBindingsBehavior,
    dayjs: NpmPackages.dayjs || backupNpmPackages.dayjs
  };
}

module.exports = {
  getGlobalHelpers,
  pathResolver,
  // 直接导出关键模块，方便直接使用
  pageHelper: pageHelper || backupHelpers.pageHelper,
  cloudHelper: cloudHelper || backupHelpers.cloudHelper,
  timeHelper: timeHelper || backupHelpers.timeHelper,
  mobx: NpmPackages.mobx || backupNpmPackages.mobx,
  createStoreBindings: NpmPackages.createStoreBindings || backupNpmPackages.createStoreBindings,
  dayjs: NpmPackages.dayjs || backupNpmPackages.dayjs
}; 